package com.easemob.helpdesk.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.easemob.helpdesk.utils.DateUtils;
import com.easemob.helpdesk.utils.PreferenceUtils;
import com.housekeeperapp.R;


/**
 * 自定义AlerDialog
 */
public class FinishSessionDialog extends BaseActivityV2 implements OnClickListener {
	/**
	 * Dialog 显示信息View
	 */
//	private TextView tvMessage;
	private LinearLayout dialog_container;

//	private TextView userName;
//	private View tvTitleLine;
	/**
	 * Dialog 确定，取消按钮
	 */
	private Button btnOk,btnCancel;
	/**
	 * 临时记录配置变量
	 */
	private int position;
	/**
	 * 文本框显示内容
	 */
//	private String textMessage;
//
//	private String textTitle;

	//临时存储order id
//	private long order_id;
	/**
	 * 确定按钮，显示文字
	 */
//	private String okString = null;bu
	private boolean clickOk = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.finish_session_dialog_layout);
		Intent gIntent = getIntent();
//		textMessage = gIntent.getStringExtra("msg");
//		okString = gIntent.getStringExtra("okString");
//		textTitle = gIntent.getStringExtra("title");
//		cancelString = gIntent.getStringExtra("cancelString");

		position = gIntent.getIntExtra("position", -1);

//		order_id = gIntent.getLongExtra("order_id", 0);
		initView();
		
	}

	/**
	 * 获取当前Dialog内的View
	 */
	private void initView() {
//		tvMessage = (TextView) findViewById(R.id.dialog_message);
//		userName = (TextView) findViewById(R.id.dialog_title);
//		tvTitleLine = findViewById(R.id.dialog_title_line);
		dialog_container = (LinearLayout) findViewById(R.id.dialog_container);

		btnOk = (Button) findViewById(R.id.ok);
		btnCancel = (Button) findViewById(R.id.cancel);

		int outerwidth = getResources().getDisplayMetrics().widthPixels;
		ViewGroup.LayoutParams layoutlp = dialog_container.getLayoutParams();
		layoutlp.width = outerwidth - 85;
		dialog_container.setLayoutParams(layoutlp);


		btnCancel.setOnClickListener(this);
		btnOk.setOnClickListener(this);
//		if(!TextUtils.isEmpty(textMessage)){
//			tvMessage.setText(Html.fromHtml(textMessage));
//		}
//		if(!TextUtils.isEmpty(okString)){
//			btnOk.setText(okString);
//		}
//
//		if(!TextUtils.isEmpty(textTitle)){
//			userName.setVisibility(View.VISIBLE);
//			tvTitleLine.setVisibility(View.VISIBLE);
//			userName.setText(textTitle);
//		}
//
//		if(!TextUtils.isEmpty(cancelString)){
//			btnCancel.setText(cancelString);
//		}
		
	}

	@Override
	public void onClick(View v) {

		if(v.getId()==R.id.ok){
			clickOk = true;
			Intent intent = getIntent().putExtra("position", position);
//			if(order_id != 0){
//				intent.putExtra("order_id", order_id);
//			}

			setResult(RESULT_OK, intent);
			if(position != -1){
				ChatActivity.resendPos = position;
			}
			finish();
		}
		else if(v.getId()==R.id.cancel){
			setResult(ChatActivity.RESULT_CANCELED_CLOSE_DIALOG, getIntent().putExtra("position", position));
			finish();
		}

	}

//	@Override
//	protected void onNewIntent(Intent intent) {
//		super.onNewIntent(intent);
//		setIntent(intent);
//		Intent gIntent = getIntent();
//		textMessage = gIntent.getStringExtra("msg");
//		okString = gIntent.getStringExtra("okString");
//		position = gIntent.getIntExtra("position", -1);
//		initView();
//	}

	@Override
	public void finish() {
		if (!clickOk){//没有点击结束会话确定按钮，点击弹窗外部或者点击不在提示按钮
			String currentDate = DateUtils.getCurrentDate();
			PreferenceUtils.getInstance().setLastCloseDialogDate(currentDate);
		}
		super.finish();
	}
}
